package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.R;
import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.ActivityWish;
import com.localjoycommunity.cpdj.project.system.domain.UserActivity;
import com.localjoycommunity.cpdj.project.system.domain.UserWish;
import com.localjoycommunity.cpdj.project.system.domain.WishList;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserPartyActivityHistoryVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserPartyActivityListVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserWishActivityListVo;
import com.localjoycommunity.cpdj.project.system.domain.vo.UserWishHistoryVo;
import com.localjoycommunity.cpdj.project.system.service.IActivityWishService;
import com.localjoycommunity.cpdj.project.system.service.IUserActivityService;
import com.localjoycommunity.cpdj.project.system.service.IUserWishService;
import com.localjoycommunity.cpdj.project.system.service.IWishListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 心愿 活动Controller
 *
 * @author aqie
 * @date 2020-11-29
 */
@RestController
@RequestMapping("/system/wish")
public class ActivityWishController extends BaseController {
    @Autowired
    private IActivityWishService activityWishService;

    @Autowired
    private IWishListService wishListService;

    @Autowired
    private IUserWishService userWishService;

    @Autowired
    private IUserActivityService userActivityService;

    /**
     * App 获取心愿/党建活动详情
     *
     * @param id
     * @return
     */
    @NeedLogin
    @GetMapping(value = "app/{id}")
    @Transactional
    public R<ActivityWish> getAppInfo(@PathVariable("id") Long id,
                                      HttpServletRequest req) {
        Integer userId = getUserId(req);
        ActivityWish activityWish = activityWishService.selectActivityWishById(id);
        activityWishService.updateActivityVisitNum(id);
        // 如果是心愿
        if (activityWish.getType() == 1) {
            WishList wishList = new WishList();
            wishList.setActivityWishId(id);
            // 添加每个用户 是否参加心愿
            List<WishList> wishLists = wishListService.selectWishListList(wishList);
            wishLists.forEach(item -> item.setIsApply(userWishService.isApplyWish(item.getActivityWishId())));
            activityWish.setWishLists(wishLists);
        } else {
            // 添加每个用户 是否参加活动
            activityWish.setIsApplyPartyActivity(userActivityService.isApplyPartyActivity(Long.valueOf(userId), id));
        }
        return R.success(activityWish);
    }

    /**
     * 认领/取消 心愿 todo 同时限制认领人数
     */
    @PostMapping("applyWish")
    @NeedLogin
    public R<Integer> applyWish(@RequestBody UserWish userWish, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userWish.setUserId((long) userId);
        int i = userWishService.applyWish(userWish);
        return R.success(i);
    }

    /**
     * 报名/取消 党建活动 todo 同时限制认领人数
     *
     * @param userActivity
     * @param req
     * @return
     */
    @PostMapping("applyPartyActivity")
    @NeedLogin
    public R<Integer> applyPartyActivity(@RequestBody UserActivity userActivity, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userActivity.setUserId(Long.valueOf(userId));
        int i = userActivityService.applyPartyActivity(userActivity);
        return R.success(i);
    }

    /**
     * 用户 党建活动列表
     *
     * @return
     */
    @GetMapping("userPartyActivity/{stateCode}")
    public R<List<UserPartyActivityListVo>> userPartyActivityList(@PathVariable("stateCode") Integer stateCode, HttpServletRequest req) {
        Integer userId = getUserId(req);
        List<UserPartyActivityListVo> list = userActivityService.userPartyActivityList(Long.valueOf(userId), stateCode);
        return R.success(list);
    }

    /**
     * 用户 心愿列表
     *
     * @return
     */
    @GetMapping("userWishActivity/{stateCode}")
    public R<List<UserWishActivityListVo>> userWishActivityList(@PathVariable("stateCode") Integer stateCode, HttpServletRequest req) {
        Integer userId = getUserId(req);
        List<UserWishActivityListVo> list = userActivityService.userWishActivityList(Long.valueOf(userId), stateCode);
        return R.success(list);
    }

    /**
     * 管理后台 查询心愿 活动列表 1-wish 2-activity
     */
    @GetMapping("/list")
    public TableDataInfo list(ActivityWish activityWish) {
        startPage();
        List<ActivityWish> list = activityWishService.selectActivityWishList(activityWish);
        return getDataTable(list);
    }

    /**
     * 导出心愿 活动列表
     */
    @Log(title = "心愿 活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActivityWish activityWish) {
        List<ActivityWish> list = activityWishService.selectActivityWishList(activityWish);
        ExcelUtil<ActivityWish> util = new ExcelUtil<ActivityWish>(ActivityWish.class);
        return util.exportExcel(list, "心愿");
    }

    /**
     * 用户心愿历史
     *
     * @return
     */
    @Log(title = "心愿 活动", businessType = BusinessType.EXPORT)
    @GetMapping("/exportUserWishHistory")
    public AjaxResult exportuUserWishHistory() {
        List<UserWishHistoryVo> list = userWishService.selectUserWishHistory();
        ExcelUtil<UserWishHistoryVo> util = new ExcelUtil<UserWishHistoryVo>(UserWishHistoryVo.class);
        return util.exportExcel(list, "微心愿历史");
    }

    /**
     * 导出活动历史列表
     */
    @Log(title = " 活动", businessType = BusinessType.EXPORT)
    @GetMapping("/exportActivityHistory")
    public AjaxResult exportActivityHistory() {
        List<UserPartyActivityHistoryVo> list = userActivityService.userPartyActivityHistoryList();
        ExcelUtil<UserPartyActivityHistoryVo> util = new ExcelUtil<UserPartyActivityHistoryVo>(UserPartyActivityHistoryVo.class);
        return util.exportExcel(list, "活动历史");
    }


    /**
     * 获取心愿 活动详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(activityWishService.selectActivityWishById(id));
    }

    /**
     * 新增心愿 活动
     */
    @Log(title = "心愿 活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityWish activityWish) {
        activityWish.setRemainNum(activityWish.getTotalPeople());
        return toAjax(activityWishService.insertActivityWish(activityWish));
    }

    /**
     * 修改心愿 活动
     */
    @Log(title = "心愿 活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityWish activityWish) {
        return toAjax(activityWishService.updateActivityWish(activityWish));
    }

    /**
     * 删除心愿 活动
     */
    @Log(title = "心愿 活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(activityWishService.deleteActivityWishByIds(ids));
    }

    /**
     * 用户心愿历史
     *
     * @return
     */
    @GetMapping("userWishHistory")
    public TableDataInfo userWishHistory() {
        List<UserWishHistoryVo> list = userWishService.selectUserWishHistory();
        return getDataTable(list);
    }

    /**
     * 用户党建活动历史
     *
     * @return
     */
    @GetMapping("userPartyActivityHistory")
    public TableDataInfo userPartyActivityHistory() {
        List<UserPartyActivityHistoryVo> list = userActivityService.userPartyActivityHistoryList();
        return getDataTable(list);
    }
}
